package com.example.smartmallserver.demos.web.controller;

import com.example.smartmallserver.demos.web.pojo.User;
import com.example.smartmallserver.demos.web.service.LoginService;
import com.example.smartmallserver.demos.web.util.JwUtils;
import com.example.smartmallserver.demos.web.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

//@CrossOrigin
@RestController
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    HttpSession session;
    @PostMapping("/passport/login")
    Result login(@RequestBody User user){
        //System.out.println("user1:  "+user1);
        User user1 = loginService.login(user.getTel());
        Map<String,Object> claims = new HashMap<>();
        claims.put("id",user1.getId());
        claims.put("tel",user1.getTel());
        Map<String,Object> res = new HashMap<>();
        res.put("id",user1.getId());
        res.put("token",JwUtils.createToken(claims));
        return Result.success(res);
        //return loginService.login(user.getTel())?Result.success():Result.successStatus(201);
    }

    @GetMapping("/user/info")
    Result getUserInfo(){
        User user = loginService.getUserInfoById(session.getAttribute("id"));
        return Result.success(user);
    }
}
